package com.yuantu.judiciary.serviceimpl;

import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.yuantu.judiciary.common.SysCode;
import com.yuantu.judiciary.exception.ServiceException;
import com.yuantu.judiciary.model.api.*;
import com.yuantu.judiciary.model.dto.CaseTaxBreakDTO;
import com.yuantu.judiciary.model.enums.SearchType;
import com.yuantu.judiciary.service.IOpenApiRequestService;
import com.yuantu.judiciary.service.IOpenApiService;
import com.yuantu.judiciary.utils.AssertUtil;
import com.yuantu.judiciary.utils.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 外部接口实现
 *
 * @author syw
 * @since 2023-06-29
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class OpenApiServiceImpl implements IOpenApiService {

    private static final Gson GSON = new Gson();
    private final IOpenApiRequestService<BaseApiRequestMsg, BaseApiResponseMsg> httpRequestService;


    @Override
    public String personBadRecord(PersonBadRecordDTO data) throws ServiceException {
        try {
            PersonBadRecordMsg requestMsg = new PersonBadRecordMsg(JSON.toJSONString(Collections.singletonList(data)), SearchType.PERSON.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String sanctionById(SanctionByIdDTO data) throws ServiceException {
        try {
            SanctionByIdMsg requestMsg = new SanctionByIdMsg(JSON.toJSONString(Collections.singletonList(data)), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String sanctionByName(SanctionByNameDTO data) throws ServiceException {
        try {
            SanctionByNameMsg requestMsg = new SanctionByNameMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String sanctionInfo(SanctionInfoDTO data) throws ServiceException {
        try {
            SanctionInfoMsg requestMsg = new SanctionInfoMsg(JSON.toJSONString(Collections.singletonList(data)), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String sanctionList(SanctionByNameDTO data) throws ServiceException {
        try {
            SanctionInfoMsg requestMsg = new SanctionInfoMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String judicialInvolvement(JudicialInvolvementDTO data) throws ServiceException {
        try {
            JudicialInvolvementMsg requestMsg = new JudicialInvolvementMsg(JSON.toJSONString(Collections.singletonList(data)), data.getType().name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String companyInfo(CompanyInfoDTO data) throws ServiceException {
        try {
            CompanyInfoMsg requestMsg = new CompanyInfoMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String limitHigh(LimitHighDTO data) throws ServiceException {
        try {
            LimitHighMsg requestMsg = new LimitHighMsg(JSON.toJSONString(Collections.singletonList(data)), data.getType().name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String dishonestInfo(DisHonestInfoDTO data) throws ServiceException {
        try {
            DishonestInfoMsg requestMsg = new DishonestInfoMsg(JSON.toJSONString(Collections.singletonList(data)), data.getType().name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String enterpriseList(EnterpriseListDTO data) throws ServiceException {
        try {
            EnterpriseListMsg requestMsg = new EnterpriseListMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String faceRecognition(PersonFaceRecognitionDTO data) throws ServiceException {
        try {
            PersonFaceRecognitionMsg requestMsg = new PersonFaceRecognitionMsg(JSON.toJSONString(data), SearchType.PERSON.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String faceRecognitionResult(PersonFaceRecognitionResultDTO data) throws ServiceException {
        try {
            PersonFaceRecognitionResultMsg requestMsg = new PersonFaceRecognitionResultMsg(JSON.toJSONString(data), SearchType.PERSON.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequestNotEncrypt(requestMsg);
//            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String personBadRecordProd(PersonBadRecordDTO data) {
        try {
            ProdPersonBadRecordMsg requestMsg = new ProdPersonBadRecordMsg(JSON.toJSONString(Collections.singletonList(data)), SearchType.PERSON.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String judicialInvolvementProd(JudicialInvolvementDTO data) {
        try {
            ProdJudicialInvolvementMsg requestMsg = new ProdJudicialInvolvementMsg(JSON.toJSONString(Collections.singletonList(data)), data.getType().name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String personBusinessProd(PersonBadRecordDTO data) {
        try {
            ProdPersonBusinessMsg requestMsg = new ProdPersonBusinessMsg(JSON.toJSONString(Collections.singletonList(data)), SearchType.PERSON.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String companyInfoProd(CompanyInfoDTO data) {
        try {
            ProdCompanyInfoMsg requestMsg = new ProdCompanyInfoMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String judicialProd(JudicialInvolvementDTO data) {
        try {
            ProdJudicialMsg requestMsg = new ProdJudicialMsg(JSON.toJSONString(Collections.singletonList(data)), data.getType().name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg.getData());
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }

    @Override
    public String enterpriseListProd(EnterpriseListDTO data) {
        try {
            ProdEnterpriseListMsg requestMsg = new ProdEnterpriseListMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
            BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
            return GSON.toJson(responseMsg);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.EXTERNAL_API_ERROR);
        }
    }


    @Override
    public String enterpriseCaseProd(EnterpriseCaseDTO data) {
        ProdEnterpriseCaseMsg requestMsg = new ProdEnterpriseCaseMsg("[" + JSON.toJSONString(data) + "]", SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        JsonArray resultJson = GSON.fromJson(responseMsg.getData().toString(), JsonArray.class);
        CompanyCaseInfoDTO companyCaseInfo = JSON.parseObject(resultJson.get(0).toString(), CompanyCaseInfoDTO.class);
        System.out.println(JSON.toJSONString(companyCaseInfo));
        return GSON.toJson(responseMsg);
    }

    @Override
    public Boolean personAddMonitorProd(MonitorPersonAddDTO data) {
        ProdPersonTimerAddMsg requestMsg = new ProdPersonTimerAddMsg("[" + JSON.toJSONString(data) + "]", SearchType.PERSON.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        System.out.println(GSON.toJson(responseMsg));
        return true;
    }

    @Override
    public Boolean personRemoveMonitorProd(MonitorPersonAddDTO data) {
        ProdPersonTimerRemoveMsg requestMsg = new ProdPersonTimerRemoveMsg("[" + JSON.toJSONString(data) + "]", SearchType.PERSON.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        System.out.println(GSON.toJson(responseMsg));
        return true;
    }

    @Override
    public MonitorFindResultDTO personFindMonitorProd(MonitorFindPageDTO data) {

        ProdPersonTimerFindAddMsg requestMsg = new ProdPersonTimerFindAddMsg(JSON.toJSONString(data), SearchType.PERSON.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        if (ObjectUtils.isEmpty(responseMsg.getData())) {
            MonitorFindResultDTO monitorFindResult = new MonitorFindResultDTO();
            monitorFindResult.setList(new ArrayList<>());
            monitorFindResult.setTotal(0L);
            return monitorFindResult;
        }
        return JSON.parseObject(responseMsg.getData().toString(), MonitorFindResultDTO.class);
    }

    @Override
    public MonitorQueryResultDTO personQueryMonitorProd(MonitorQueryPageDTO data) {

        ProdPersonTimerQueryMsg requestMsg = new ProdPersonTimerQueryMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        if (ObjectUtils.isEmpty(responseMsg.getData())) {
            MonitorQueryResultDTO monitorQueryResult = new MonitorQueryResultDTO();
            monitorQueryResult.setRecords(new ArrayList<>());
            monitorQueryResult.setCurrent(0L);
            return monitorQueryResult;
        }
        return JSON.parseObject(responseMsg.getData().toString(), MonitorQueryResultDTO.class);
    }

    @Override
    public Boolean enterAddMonitorProd(MonitorEnterAddDTO data) {
        ProdEnterTimerAddMsg requestMsg = new ProdEnterTimerAddMsg("[" + JSON.toJSONString(data) + "]", SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        System.out.println(GSON.toJson(responseMsg));
        return true;
    }

    @Override
    public Boolean enterRemoveMonitorProd(MonitorEnterAddDTO data) {

        ProdEnterTimerRemoveMsg requestMsg = new ProdEnterTimerRemoveMsg("[" + JSON.toJSONString(data) + "]", SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        System.out.println(GSON.toJson(responseMsg));
        return true;
    }

    @Override
    public MonitorFindResultDTO enterFindMonitorProd(MonitorFindPageDTO data) {
        ProdEnterTimerFindAddMsg requestMsg = new ProdEnterTimerFindAddMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        if (ObjectUtils.isEmpty(responseMsg.getData())) {
            MonitorFindResultDTO monitorFindResult = new MonitorFindResultDTO();
            monitorFindResult.setList(new ArrayList<>());
            monitorFindResult.setTotal(0L);
            return monitorFindResult;
        }
        return JSON.parseObject(responseMsg.getData().toString(), MonitorFindResultDTO.class);
    }

    @Override
    public MonitorQueryResultDTO enterQueryMonitorProd(MonitorQueryPageDTO data) {

        ProdEnterTimerQueryMsg requestMsg = new ProdEnterTimerQueryMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(requestMsg);
        if (ObjectUtils.isEmpty(responseMsg.getData())) {
            MonitorQueryResultDTO monitorQueryResult = new MonitorQueryResultDTO();
            monitorQueryResult.setRecords(new ArrayList<>());
            monitorQueryResult.setCurrent(0L);
            return monitorQueryResult;
        }
        return JSON.parseObject(responseMsg.getData().toString(), MonitorQueryResultDTO.class);
    }

    @Override
    public String enterQueryTaxBreakProd(CompanyTaxBreakQueryDTO data) {
        ProdCompanyTaxBreakMsg prodCompanyTaxBreak = new ProdCompanyTaxBreakMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(prodCompanyTaxBreak);
        if (ObjectUtils.isEmpty(responseMsg.getData())) {
            return "{}";
        }
        return GSON.toJson(responseMsg.getData());
    }

    @Override
    public String enterQueryAdminPunishProd(CompanyAdminPunishPageQueryDTO data) {
        ProdCompanyAdminPunishMsg prodCompanyAdminPunish = new ProdCompanyAdminPunishMsg(JSON.toJSONString(data), SearchType.ENTERPRISE.name());
        BaseApiResponseMsg responseMsg = httpRequestService.emitHttpRequest(prodCompanyAdminPunish);
        if (ObjectUtils.isEmpty(responseMsg.getData())) {
            return "{}";
        }
        return GSON.toJson(responseMsg.getData());
    }

}
